import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import('@/components/Login.vue')
const Home = () => import('@/components/Home.vue')
const Welcome = () => import('@/components/Welcome.vue')
const Users = () => import('@/components/users/Users.vue')
const Rights = () => import('@/components/power/Rights.vue')
const Roles = () => import('@/components/power/Roles.vue')
const Cate = () => import('@/components/goods/Cate.vue')
const Params = () => import('@/components/goods/Params.vue')
const List = () => import('@/components/goods/List.vue')
const Add = () => import('@/components/goods/Add.vue')
const Detail = () => import('@/components/goods/Detail.vue')
const Order = () => import('@/components/order/Order.vue')
const Report = () => import('@/components/report/Report.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录'
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    //默认到welcome页面
    redirect: '/welcome',
    meta: {
      title: '首页'
    },
    children: [
      {
        path: '/welcome',
        component: Welcome,
        meta: {
          title: '欢迎页'
        }
      },
      {
        path: '/users',
        component: Users,
        meta: {
          title: '用户列表'
        }
      },
      {
        path: '/rights',
        component: Rights,
        meta: {
          title: '权限列表'
        }
      },
      {
        path: '/roles',
        component: Roles,
        meta: {
          title: '角色列表'
        }
      },
      {
        path: '/categories',
        component: Cate,
        meta: {
          title: '商品分类'
        }
      },
      {
        path: '/params',
        component: Params,
        meta: {
          title: '商品参数'
        }
      },
      {
        path: '/goods',
        component: List,
        meta: {
          title: '商品列表'
        }
      },
      {
        path: '/goods/add',
        component: Add,
        meta: {
          title: '商品添加'
        }
      },
      {
        path: '/goods/detail',
        component: Detail,
        meta: {
          title: '商品详情'
        }
      },
      {
        path: '/orders',
        component: Order,
        meta: {
          title: '订单列表'
        }
      },
      {
        path: '/reports',
        component: Report,
        meta: {
          title: '报表分析'
        }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  //给页面添加指定路由对应的标题
  window.document.title = to.meta.title
  console.log(from)
  let path = to.path
  //如果是到登录界面，直接放行
  if (path === '/' || path === '/login') {
    return next()
  }
  //到本地存储中获得用户登录信息
  let str = sessionStorage.getItem('USER_LOGIN')
  if (!str) {
    return next('/login')
  }
  let data = JSON.parse(str)
  if (!data) {
    return next('/login')
  }
  let token = data.token
  if (!token) {
    return next('/login')
  }
  //放行
  return next()
})
export default router
